package com.digitalchina.lxnsy.activity.mainConsult;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.ConsultIssueV2Activity;
import com.digitalchina.lxnsy.base.BaseV4Fragment;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.ExpertNewVO;
import com.digitalchina.lxnsy.entity.MainCosultVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.xml.UserXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.fragment
 * 创建人：郑皓
 * 创建时间：2018/8/8 15:31
 * 修改备注：
 * ================================================
 */
public class ConsultFragment extends BaseV4Fragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String type = "";

    private List<MainCosultVO> voList = new ArrayList<>();

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_collect);
    }

    @Override
    public void initMember() {

        type = getArguments().getString("type");

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 10.0f, 0.0f, 0.0f));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ConsultHolder(parent);
            }
        });

        onRefresh();

    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {

                MainCosultVO vo = (MainCosultVO) adapter.getAllData().get(position);
                //会话状态 0-未提问 1-专家已回复 2-待回复 3-待评价 4-已评价 5-已失效
                if("0".equals(vo.getSessionStatus())){
                    ExpertNewVO expertVO = new ExpertNewVO();
                    expertVO.setConsultIsFee(vo.getIsFee());
                    expertVO.setOrderNo(vo.getOrderNo());
                    expertVO.setId(vo.getExpertId());
                    GotoUtil.gotoActivity(getActivity(), ConsultIssueV2Activity.class, "single", expertVO);
                }else{
                    GotoUtil.gotoActivity(getActivity(), ConsultDetailActivity.class,  "vo", vo);
                }
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG_S_0:
                        callBack(msg.obj);
                        break;
                    case MSG_F_0:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        voList = FastJsonUtil.getListBean(object.toString(), "consultSessionInfoList", MainCosultVO.class);

        if (voList == null)
            return;

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList.size() < limit) {
            adapter.stopMore();
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            getData();
        }
    }

    private void getData() {
        if ("1".equals(UserXML.getLoginStatus(getActivity()))) {
            HttpParams params = new HttpParams();
            params.put("userAccId", UserXML.getUserId(getActivity()));
            params.put("type", type);
            params.put("page", String.valueOf(page));
            params.put("limit", String.valueOf(limit));

            OkHttpUtil.post(getActivity(), URL.URL_GET_SESSION_LIST, "", params, mHandler,
                    MSG_S_0, MSG_F_0);
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        onRefresh();
    }
}
